package richbar.com.richbar.activity.login;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.MainActivity;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.me.AccountRelationAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.BaseBean;
import richbar.com.richbar.bean.BindTelBean;
import richbar.com.richbar.bean.CodeBean;
import richbar.com.richbar.bean.LoginBean;
import richbar.com.richbar.bean.WxUserInfoResult;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.SpUtil;
import richbar.com.richbar.util.Util;


/**
 * Created by QiYang on 2018/12/24.
 */

public class BindTelAc extends BaseActivity {
    @BindView(R.id.tel)
    EditText tel_et;
    @BindView(R.id.code)
    EditText code_et;
    @BindView(R.id.get_code)
    TextView getCode;
    WxUserInfoResult result;
    private LoadingDialog loadingDialog;

    @Override
    public int initLayout() {
        return R.layout.ac_bind_tel;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("绑定微信");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);
    }

    @Override
    public void initData() {
        result = (WxUserInfoResult) getBundle().getSerializable("result");
        LogUtil.e("WDAWFHWIWIFWF", result.getOpenid());
    }


    @OnClick({R.id.get_code, R.id.complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_code:
                if(tel_et.getText().toString().length()==11)
                {
                    isBandTel(tel_et.getText().toString());

                }
                else
                {
                    ToastUtil.showToast(BindTelAc.this, "请填写正确的手机号码");
                }

                break;
            case R.id.complete:
                submit();
                break;
        }
    }

    /**
     * 判断该手机号是否绑定
     * @param tel
     */
    private void isBandTel(String tel)
    {
        loadingDialog.show();
        NetWorkUtil.BundLingTeL(tel, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                final BindTelBean bindTelBean = MyGson.getInstance().fromJson(data, BindTelBean.class);
                if(bindTelBean.getData().getStatus().equals("1"))
                {
                    ToastUtil.showToast(BindTelAc.this, "该手机号已经绑定");
                }
                else
                {
                    getCode();
                    new CountDownTimerHelp(BindTelAc.this, getCode, 60000, 1000).start();
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                loadingDialog.dismiss();
                ToastUtil.showToast(BindTelAc.this, "msg");
            }
        });

    }

    private void getCode() {
        if (StringUtil.getStr(tel_et).length() == 11) {
            if (!Util.checkNet(getApplicationContext())) {
                ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
            } else {

                if (AccountRelationAc.isBand) {
                    NetWorkUtil.BandWx(result.getOpenid(), result.getNickname(), result.getHeadimgurl(), new HttpUtils.HttpCallback() {
                        @Override
                        public void onSuccess(String data) {
                            final BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                            ToastUtil.showToast(BindTelAc.this, "默认密码为手机后6位");
                        }

                        @Override
                        public void onError(String msg) {
                            super.onError(msg);
                        }
                    });
                    AccountRelationAc.isBand = false;
                } else {
                    NetWorkUtil.getRegsinCode(Util.md5(), StringUtil.getStr(tel_et), "8", new HttpUtils.HttpCallback() {
                        @Override
                        public void onSuccess(String data) {
                            LogUtil.e("WDWDAWDAWD", data);
                            final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                            ToastUtil.showToast(BindTelAc.this, codeBean.getMsg());
                        }

                        @Override
                        public void onError(String msg) {
                            super.onError(msg);
                        }
                    });
                }

            }


        } else {
            ToastUtil.showToast(BindTelAc.this, "请输入正确的手机号");
        }
    }

    private void submit() {
        String tel = StringUtil.getStr(tel_et);
        String code = StringUtil.getStr(code_et);
        if (StringUtil.isEmpty(tel, code)) {
            ToastUtil.showToast(BindTelAc.this, "请填写完整");
            return;
        }
        loadingDialog.show();
        NetWorkUtil.bindWx(tel, code, result.getOpenid(), result.getNickname(), result.getHeadimgurl(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                final LoginBean result = MyGson.getInstance().fromJson(data, LoginBean.class);
                if (null != result) {
                    ToastUtil.showToast(BindTelAc.this, "绑定成功");
                    BaseApplication.uid = result.getData().getUid();
                    SpUtil.setLoginInfo(BindTelAc.this, result);
                    startF(MainActivity.class);
                }
            }
        });
    }
}
